package org.apache.ibatis.ibator.plugins;

import java.util.List;

import org.apache.ibatis.ibator.api.IbatorPluginAdapter;
import org.apache.ibatis.ibator.api.IntrospectedTable;
import org.apache.ibatis.ibator.api.dom.java.Interface;
import org.apache.ibatis.ibator.api.dom.xml.Document;
import org.apache.ibatis.ibator.api.dom.xml.XmlElement;
import org.apache.ibatis.ibator.generator.ibatis3.pagination.Ibatis3PaginationGenerator;
import org.apache.ibatis.ibator.generator.ibatis3.pagination.javamapper.JavaMapperPaginationGenerator;
import org.apache.ibatis.ibator.internal.util.StringUtility;

/**
 * 分页插件
 * 
 * @author liwb 2016年1月27日 下午4:00:10
 */
public class PaginationPlugin extends IbatorPluginAdapter {
    /** 是否分页 */
    private String enablePagination;
    /** 数据库类型 */
    private String databaseType;

    @Override
    public boolean validate(List<String> warnings) {
        databaseType = properties.getProperty("databaseType");
        enablePagination = properties.getProperty("enablePagination");

        boolean valid = StringUtility.stringHasValue(databaseType)
                && StringUtility.stringHasValue(enablePagination);

        return valid;
    }

    @Override
    public boolean sqlMapDocumentGenerated(Document document,
            IntrospectedTable introspectedTable) {
        XmlElement element = document.getRootElement();
        if (StringUtility.stringHasValue(enablePagination)
                && "true".equalsIgnoreCase(enablePagination)) {
            if ("oracle".equalsIgnoreCase(databaseType)) {
                // 增加手写的头sql
                // AbstractXmlElementGenerator oraclePaginationTailGenerator =
                // new OraclePaginationTailGenerator();
                // oraclePaginationTailGenerator.setIbatorContext(ibatorContext);
                // oraclePaginationTailGenerator
                // .setIntrospectedTable(introspectedTable);
                // oraclePaginationTailGenerator.addElements(element);
                //
                // // 增加手写的尾sql
                // AbstractXmlElementGenerator oraclePaginationHeadGenerator =
                // new OraclePaginationHeadGenerator();
                // oraclePaginationHeadGenerator.setIbatorContext(ibatorContext);
                // oraclePaginationHeadGenerator
                // .setIntrospectedTable(introspectedTable);
                // oraclePaginationHeadGenerator.addElements(element);
            } else if ("mysql".equalsIgnoreCase(databaseType)) {
                if ("Ibatis3".equals(ibatorContext.getTargetRuntime())) {
                    new Ibatis3PaginationGenerator().xmlMySqlGenerator(
                            ibatorContext, introspectedTable, element);
                }
            }
        }

        return super.sqlMapDocumentGenerated(document, introspectedTable);
    }

    public boolean daoInterfaceGenerated(Interface interfaze,
            IntrospectedTable introspectedTable) {
        if ("Ibatis3".equals(ibatorContext.getTargetRuntime())) {
            new JavaMapperPaginationGenerator(interfaze, introspectedTable,
                    ibatorContext).generator();
        }
        return super.daoInterfaceGenerated(interfaze, introspectedTable);
    }
}
